﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NHibernate;
using NHibernate.Criterion;

using Revis.VClinicNet.Core.Domain.Repository;

namespace Revis.VClinicNet.Core.Domain.Persistence.Nh
{
    public class VisitRepository : RepositoryBase<Visit>, IVisitRepository
    {
        public VisitRepository(ISessionFactory factor) :
            base(factor)
        {
        }

        public IList<Visit> FindDoctorVisits(long doctorId)
        {
            IList<Visit> visits = Session.CreateCriteria<Visit>("v")
                .Add(Expression.Eq("v.Doctor.Id", doctorId)).List<Visit>();
            return visits;
        }

        public IList<Visit> FindDoctorVisits(long doctorId, DateTime date)
        {
            IList<Visit> visits = Session.CreateCriteria<Visit>("v")
                .Add(Expression.Eq("v.Doctor.Id", doctorId))
                .Add(Expression.Ge("v.Date", date.Date))
                .Add(Expression.Lt("v.Date", date.Date.AddDays(1)))
                .SetFetchMode("v.Doctor", FetchMode.Eager)
                .SetFetchMode("v.Patient", FetchMode.Eager)
                .List<Visit>();
            return visits;
        }


        public IList<Visit> FindPatientVisits(long patientId)
        {
            return Session.CreateCriteria<Visit>("v")
                .Add(Expression.Eq("v.Patient.Id", patientId))
                .SetFetchMode("v.Doctor", FetchMode.Eager)
                .SetFetchMode("v.Patient", FetchMode.Eager)
                .List<Visit>();
        }
    }
}
